/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.depot;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import model.entity.Depot;
import model.entity.Vehicle;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Named(value = "depotCreateMB")
@SessionScoped
public class DepotCreateMB implements Serializable {

    /**
     * Creates a new instance of DepotCreateMB
     */
    public DepotCreateMB() {
    }
    @EJB(beanName = "VehicleManager")
    private InterfaceServiceLocal<Vehicle> vehicleManager;
    @EJB(beanName = "DepotManager")
    private InterfaceServiceLocal<Depot> depotManager;
    private Depot currentdepot;
    private List<Vehicle> vehicleList;

    public void init() {
        currentdepot = new Depot();
        vehicleList = new ArrayList<Vehicle>();

        currentdepot.setVehicleList(vehicleList);
    }

    public void precreateDepot() {
        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        init();

    }

    public void create() {
        depotManager.create(currentdepot);
        List<Vehicle> vehicleList2 = new ArrayList<Vehicle>();
        vehicleList.forEach(s -> {
            s.setIddepot(currentdepot);
            Vehicle edit = vehicleManager.edit(s);
            //edit.setIddepot(currentdepot);
            vehicleList2.add(edit);
        });
        currentdepot.setVehicleList(vehicleList2);
        depotManager.edit(this.currentdepot);
        init();
        Messages.create(Faces.getResourceBundle("bundle").getString("saveSuccess")).add();
    }

    public Depot getCurrentdepot() {
        return currentdepot;
    }

    public void setCurrentdepot(Depot currentdepot) {
        this.currentdepot = currentdepot;
    }

    public List<Vehicle> getVehicleList() {
        return vehicleList;
    }

    public void setVehicleList(List<Vehicle> vehicleList) {
        this.vehicleList = vehicleList;
    }

}
